/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2014 Dameng Database Inc.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.dm.demo.web.controller;

import com.dm.demo.service.bo.IdeaBo;
import com.dm.demo.service.service.IdeaService;
import com.dm.demo.web.aspects.NotifyClients;
import com.dm.demo.web.vo.IdeaVo;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Title.
 * <p/>
 * Description.
 *
 * @author Bill Lv {@literal <billcc.lv@hotmail.com>}
 * @version 1.0
 * @since 2014-12-05
 */
@Controller
@RequestMapping("/")
public class IdeaController {
    @Autowired
    private IdeaService service;

    @Autowired
    private Mapper mapper;

    @RequestMapping(method = RequestMethod.GET)
    public String viewIdeas() {
        return "ideas";
    }

    @RequestMapping(value = "/ideas", method = RequestMethod.GET)
    @ResponseBody
    public List<IdeaVo> getIdeas() {
        List<IdeaBo> bos = service.getIdeas();
        List<IdeaVo> vos = new ArrayList<>(bos.size());
        for (IdeaBo bo : bos) {
            vos.add(mapper.map(bo, IdeaVo.class));
        }
        return vos;
    }

    @NotifyClients
    @RequestMapping(value = "/ideas/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public IdeaVo update(@PathVariable int id, @RequestBody IdeaVo idea) {
        IdeaBo bo = mapper.map(idea, IdeaBo.class);
        bo.setId(id);
        IdeaBo outBo = service.updateIdea(bo);
        return mapper.map(outBo, IdeaVo.class);
    }

    @NotifyClients
    @RequestMapping(value = "/ideas", method = RequestMethod.POST)
    @ResponseBody
    public IdeaVo add(@RequestBody IdeaVo idea) {
        IdeaBo bo = mapper.map(idea, IdeaBo.class);
        IdeaBo outBo = service.addIdea(bo);
        return mapper.map(outBo, IdeaVo.class);
    }

    @NotifyClients
    @RequestMapping(value = "/ideas/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable int id) {
        IdeaBo task = new IdeaBo();
        task.setId(id);
        service.deleteIdea(task);
    }
}
